//
//  UIApplication+Extension.swift
//  TuneMate
//
//  Created by 丸子 on 2024/2/23.
//

import UIKit

extension UIApplication {
    /// Sweeter: `keyWindow` for scene-based apps
    var legacyKeyWindow: UIWindow? {
        if #available(iOS 13, *) {
            return windows.first { $0.isKeyWindow }
        } else {
            return keyWindow
        }
    }

    /// Sweeter: Returns the currently top-most view controller.
    class func topViewController(base: UIViewController? = UIApplication.shared.legacyKeyWindow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(base: selected)
            }
        }
        if let presented = base?.presentedViewController {
            return topViewController(base: presented)
        }
        return base
    }

    /// Sweeter: Show `viewController` over the top-most view controller.
    class func present(_ viewController: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil) {
        DispatchQueue.main.async {
            topViewController()?.present(viewController, animated: animated, completion: completion)
        }
    }
}

import Foundation

var notificationKey :Void?
extension NotificationCenter {

    /**< 监听 */
    public func addObserver_k(
        observer: NSObject,
        name: String,
        object: Any?,
        usingBlock: @escaping (Notification) -> Void) {
            
            /// object全部绑定在ViewController ios 11以后可以不需要释放观察者
            let object = NotificationCenter.default.addObserver(
                forName: NSNotification.Name(rawValue: name),
                object: object,
                queue: nil,
                using: usingBlock
            )
            
            let dict = objc_getAssociatedObject(observer, &notificationKey)
            var dictionary = dict as? Dictionary<String, Any> ?? [:]
            dictionary[name] = object
            objc_setAssociatedObject(observer, &notificationKey, dictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }

}

func wk_addNotificationClosure (
    _ observer: NSObject,
    name: String,
    object: Any? = nil,
    usingBlock: @escaping (Notification) -> Void) {
    NotificationCenter.default.addObserver_k(observer: observer, name: name, object: object, usingBlock: usingBlock)
}

func wk_addNotificationSEL(
    observer: Any,
    selector: Selector,
    name: String,
    object: Any? = nil) {
    let notificationName = NSNotification.Name(rawValue: name)
    NotificationCenter.default.addObserver(observer, selector: selector, name: notificationName, object: object)
}

func wk_postNotification(_ name: String, object: Any? = nil) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: name), object: object)
}
